package com.deyuanyun.pic.pbf.service.impl;

import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.pbf.mapper.ContrastiveAnalysisMapper;
import com.deyuanyun.pic.pbf.service.ContrastiveAnalysisService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模块描述
 *
 * @author scalpel.王敏
 * @version [v1.0] 创建时间（2016/7/4）
 * @see // 引用的类
 * Method List:
 * 1.----------------
 * 2.----------------
 * History:
 */
@Service
public class ContrastiveAnalysisServiceImpl implements ContrastiveAnalysisService {

    @Resource
    private ContrastiveAnalysisMapper contrastiveAnalysisMapper;

    @Override
    public Map<String, Object> query(Map<String, Object> param) {
        Map<String ,Object> result=new HashMap<String,Object>();
        List<Map<String,Object>> taskList= contrastiveAnalysisMapper.getNewTask(param);
        if(taskList.size() != 2)
        {
            Asserts.assertTrue(false,"没有可比较的检测信息");
        }

        //第一次检测任务 缺陷信息查询
        param.put("taskId",taskList.get(0).get("ID"));
        result.put("twoBar",queryBar(param));
        result.put("two",contrastiveAnalysisMapper.query(param));
        result.put("twoDate", taskList.get(0).get("DDATE"));

        //第二次检测任务 缺陷信息查询

        param.put("taskId",taskList.get(1).get("ID"));
        result.put("oneBar",queryBar(param));
        result.put("one",contrastiveAnalysisMapper.query(param));
        result.put("oneDate",taskList.get(1).get("DDATE"));
        return result;
    }


    private Map<String, Object> queryBar(Map<String, Object> param) {
        Map<String,Object> result=new HashMap<String,Object>();
        //查询管道或者管段的里程数，以及起始位置的绝对里程
        Map<String,Object> mileage=contrastiveAnalysisMapper.queryMileage(param);

        BigDecimal beginMileage= (BigDecimal) mileage.get("BEGINMILEAGE");
        BigDecimal lengthMileage= (BigDecimal) mileage.get("LENGTHMILEAGE");

        List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
        Map<String,Object> info=null;
        BigDecimal df=lengthMileage.divide(BigDecimal.valueOf(10));
        for(int i=0;i<10;i++)
        {

            info=new HashMap<String,Object>();
            info.put("name","第"+(i+1)+"段");
            info.put("end",false);
            info.put("b",beginMileage.add(df.multiply(BigDecimal.valueOf(i))));
            info.put("e",beginMileage.add(df.multiply(BigDecimal.valueOf(i+1))));
            if(i == 9)
            {
                info.put("end","true");
            }

            data.add(info);
        }
        param.put("data",data);


        //查询内部金属损失
        param.put("type","1");

        result.put("nbjs",contrastiveAnalysisMapper.queryBar(param));
        //查询外部金属损失
        param.put("type","2");
        result.put("wbjs",contrastiveAnalysisMapper.queryBar(param));

        //查询凹陷
        param.put("type","3");
        result.put("ax",contrastiveAnalysisMapper.queryBar(param));

        //查询焊缝缺陷
        param.put("type","4");
        result.put("hf",contrastiveAnalysisMapper.queryBar(param));

        //查询螺旋焊缝
        param.put("type","5");
        result.put("lxhf",contrastiveAnalysisMapper.queryBar(param));

        return result;
    }
}
